import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2023-09-01
 * Time: 9:38
 */
public class Test1 {
    public static void main1(String[] args) {
        Map<String,Integer> map=new TreeMap<>();//实现了Map接口
        map.put("qwer",123);
        map.put("qwerfe",1323);
        map.put("qwerw",1223);
        map.put("qwerfe",132553);
        System.out.println(map.get("qwer"));
        System.out.println(map.getOrDefault("qwerw", 111));
        System.out.println(map.getOrDefault("qwerw232", 111));
        map.put("qwertyuiop",456);
        System.out.println(map.get("qwertyuiop"));
        System.out.println("**************");
        map.remove("qwerw");
        System.out.println(map.get("qwerw"));
        System.out.println(map.keySet());
        Set<String> set=map.keySet();
        System.out.println("$$$$$$$$$$$$$$$$$$$$$");
        //values()是将map中的value放在collect的一个集合中返回的
        Collection<Integer> collection=map.values();

        System.out.println(map.entrySet());

        Set<Map.Entry<String,Integer>> entrySet=map.entrySet();
        for (Map.Entry<String,Integer> entry:
             entrySet) {
            System.out.println(entry.getKey()+" "+entry.getValue());
        }

        System.out.println("&&&&&&&&&&&&&&&&&&&");
        System.out.println(map.containsKey("rtyu"));
        System.out.println(map.containsValue(456));
        map.put(null,123);


        System.out.println("ertyui");

    }


    public static void main2(String[] args) {
        Set<String> set=new TreeSet<>();
        set.add("qwer");
        set.add("qwerq");
        set.add("wsqwer");
        Iterator<String> iterator=set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
        //set.add(null);
    }

    public static void main3(String[] args) {
        HashBucket hashBucket=new HashBucket();
        hashBucket.put(1,11);
        hashBucket.put(12,44);
        hashBucket.put(2,449);
        hashBucket.put(14,5);
        hashBucket.put(18,1);
        hashBucket.put(14,11);
        System.out.println(hashBucket.get(14));
        System.out.println("qwertyu");
    }
    static class   Person{
        public String id;

        public Person(String id) {
            this.id = id;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Person person = (Person) o;
            return Objects.equals(id, person.id);
        }

        @Override
        public int hashCode() {//用
            return Objects.hash(id);
        }

        @Override
        public String toString() {
            return "Person{" +
                    "id='" + id + '\'' +
                    '}';
        }
    }

    public static void main567(String[] args) {
        Person person1=new Person("123");
        Person person2=new Person("123");
        System.out.println(person1.hashCode());
        System.out.println(person2.hashCode());

        HashBucketA<Person,String> hashBucketA=new HashBucketA<>();
        hashBucketA.put(person1,"张三");
        System.out.println(hashBucketA.get(person2));
    }


    public static void main45(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str1 = in.nextLine();
            String str2 = in.nextLine();
            func(str1,str2);
        }
    }
    static void func(String str1,String str2){
        Set<Character> set=new HashSet<>();
        for (char ch:
             str2.toUpperCase().toCharArray()) {//将实际输出的转换为大写字符，并放置到hashset中
            set.add(ch);
        }
        Set<Character> setBroken=new HashSet<>();
        for (char ch:
             str1.toUpperCase().toCharArray()) {
            if(!set.contains(ch) && !setBroken.contains(ch)){//第二个条件用于重复出现的字符不会进入if语句内
                setBroken.add(ch);
                System.out.print(ch);
            }
        }
    }

}
class Test11{
    static void fun2(int[] array){
        Map<Integer,Integer> map=new HashMap<>();
        for (int i = 0; i < array.length; i++) {
            if(map.get(array[i])==null){//以前没有，就把它放进来
                map.put(array[i],1 );
            }else{//以前有，就将他的val值+1
                int val=map.get(array[i]);
                map.put(array[i],val+1);
            }
        }
        System.out.println(map);
    }

    public static void main(String[] args) {
        int[] array={1,2,3,2,1,2,1};
        fun2(array);
    }
}


class Test23{
    public static List<String> topKFrequent(String[] words, int k) {
        //1、每个单词出现的次数
        Map<String,Integer> map = new HashMap<>();
        for(String s : words) {
            if(map.get(s) == null) {
                map.put(s,1);
            }else {
                //以前有
                int val = map.get(s);
                map.put(s,val+1);
            }
        }

        //2、已经统计完毕，创建小根堆
        PriorityQueue<Map.Entry<String,Integer>> minHeap = new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if(o1.getValue().compareTo(o2.getValue()) == 0) {
                    return o2.getKey().compareTo(o1.getKey());//根据字母顺序 变成大根堆
                }
                return o1.getValue().compareTo(o2.getValue());
            }
        });

        //3.遍历map
        for (Map.Entry<String,Integer> entry: map.entrySet()) {
            if(minHeap.size()<k){
                minHeap.offer(entry);
            }else{//小根堆已满
                Map.Entry<String,Integer> top=minHeap.peek();
                if(top.getValue().compareTo(entry.getValue())==0){//频率一样的情况下，判断key值，key小的入
                    if(top.getKey().compareTo(entry.getKey())>0){
                        //entry 进
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                }else{//val值不等，只有大于堆顶元素的频率，才有资格入堆
                    if(top.getValue().compareTo(entry.getValue())<0){
                        minHeap.poll();
                        minHeap.offer(entry);
                    }

                }
            }
        }

        //4 将小根堆中的元素拿出，放到一个集合里，之后逆置
        List<String> list=new ArrayList<>();
        for (int i = 0; i <k; i++) {
            String s=minHeap.poll().getKey();
            list.add(s);
        }
        Collections.reverse(list);
        return list;
        }

    public static void main(String[] args) {
        String[] strings={"a","b","c","c"};
        List<String> ret=topKFrequent(strings,3);
        System.out.println(ret);
    }
    }